<%@ page language="java" pageEncoding="UTF-8"
         contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <%@ include file="/common/common.jsp" %>
    <meta name="format-detection"
          content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="${ctx }/statics/aui/css/aui.css"/>
    <link rel="stylesheet" type="text/css" href="${ctx }/css/weui.min.css"/>
    <script type="text/javascript" src="${ctx }/statics/libs/jquery.min.js"></script>
    <title>退款</title>
    <style type="text/css">
        .refresh_test {
            font-family: PingFangSC-Regular;
            font-size: 14px;
            color: #666666;
            line-height: 21px;
            text-align: center;
        }

        .receivables_error {
            font-family: PingFangSC-Regular;
            font-size: 18px;
            color: #333333;
            text-align: center;
        }

        .receivables_text {
            font-family: PingFangSC-Regular;
            font-size: 15px;
            color: #666666;
            line-height: 30px;
            text-align: right;
        }

        .receivables_amt {
            font-family: PingFangSC-Regular;
            font-size: 20px;
            color: #333333;
            line-height: 21px;
            text-align: right;
        }
    </style>
</head>

<body>
<form id="refundform" name="refundform" method="post">
    <input type="hidden" name="token" value="${token}"/>
    <input type="hidden" id="recordno" name="recordno"
           value="${refundNo }"><!-- 退款单号 -->
    <input type="hidden" id="sumAmount" name="sumAmount"
           value="${dateGrouping.amount }"><!-- 订单总金额 -->
    <input type="hidden" id="id" name="id"
           value="${dateGrouping.id }"><!-- 订单id-->

    <section class="aui-content">
        <ul class="aui-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-media" style="width: 6rem;">退款单号</div>
                    <div class="aui-list-item-right  ">${refundNo }</div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-media" style="width: 6rem;">原订单金额</div>
                    <div class="aui-list-item-right  ">${dateGrouping.amount }</div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-media" style="width: 6rem;">已退款金额</div>
                    <div class="aui-list-item-right  ">${refundSumAmount }</div>
                </div>
            </li>

        </ul>
    </section>

    <section class="aui-content" style="margin-top: 10px;">
        <ul class="aui-list">

            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <input type="hidden" id="amount" name="amount" value="${dateGrouping.amount }"
                           onkeyup="clearNoNum(this)" style="text-align: right;"
                           placeholder="请输入退款金额">
                    <div class="aui-list-item-media" style="width: 6rem;">退款金额</div>
                    <%--<div class="aui-list-item-inner"  >
                        <input type="text" id="amount" name="amount" value="${dateGrouping.amount }" onkeyup="clearNoNum(this)" style="text-align: right;"
                               placeholder="请输入退款金额">
                    </div>--%>
                    <div class="aui-list-item-right" style="font-size: 20px">${dateGrouping.amount }</div>
                </div>
            </li>
        </ul>
    </section>
    <section class="aui-content"
             style="margin-top: 14px; padding-left: 20px; padding-right: 20px;">
        <p>
            <c:if test="${!tkButtonShow}">
        <div class="aui-btn aui-btn-info  aui-btn-block" onclick="showMessage();">退款</div>
        </c:if>
        <c:if test="${tkButtonShow}">
            <div class="aui-btn aui-btn-info  aui-btn-block" onclick="doRefund()">退款</div>
        </c:if>
        </p>
    </section>
</form>

<div id="dialogs">
    <div class="js_dialog" id="iosDialog2" style="display: none;">
        <div class="weui-mask"></div>
        <div class="weui-dialog">
            <div class="weui-dialog__bd" id="DialogMessage"></div>
            <div class="weui-dialog__ft">
                <a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary DeleteDefault">确定</a>
            </div>
        </div>
    </div>
</div>

</body>
<script type="text/javascript" src="${ctx }/statics/aui/script/api.js"></script>
<script>

	var $iosDialog2 = $('#iosDialog2');
	$('#dialogs').on('click', '.DeleteDefault', function () { //取消按钮事件
	    $(this).parents('.js_dialog').fadeOut(200);
	});
	
	function showMessage(){
		$("#DialogMessage").html("该订单已全部退款，无法再次申请退款");
	    $iosDialog2.fadeIn(200);
	}

    function doRefund() {
        var refundAmount = $("#amount").val();///退款金额
        var amount =${dateGrouping.amount };//订单总金额
        var token = "${token}";
        if (refundAmount <= amount) { //退款金额不能大于订单金额
            var data = {recordno: $("#recordno").val(), id: $("#id").val(), amount: $("#amount").val()};
            $.ajax({
                type: "POST",
                url: "${ctx }/payorder/refund?token=" + token,
                data: JSON.stringify(data),
                headers: {token: token},
                contentType: 'application/json;charset=utf-8',
                dataType: "json",
                success: function (result) {
                    if (result.code == 0) {
                        location.href = "${ctx}/trading/center";
                    } else {
                    	$("#DialogMessage").html(result.msg);
                        $iosDialog2.fadeIn(200);
                    }
                }, error: function (e) {
                	$("#DialogMessage").html("无需重复提交");
                    $iosDialog2.fadeIn(200); 
                }
            });
        } else {
        	$("#DialogMessage").html("退款金额不能大于原订单金额");
            $iosDialog2.fadeIn(200); 
        }
    }

    function clearNoNum(obj) {
        obj.value = obj.value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符
        obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
        obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数
        if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
            obj.value = parseFloat(obj.value);
        }
        var refundAmount = $("#amount").val();///退款金额
        var amount =${dateGrouping.amount };//订单总金额
        if (refundAmount > amount) { //退款金额不能大于订单金额
            obj.value = amount;
        }
    }
</script>
</html>
